package com.wfit.controller.admin;

import com.wfit.dto.CommodityDto;
import com.wfit.dto.CommodityListDto;
import com.wfit.dto.CommodityUpdateDto;
import com.wfit.po.Commodity;
import com.wfit.result.Result;
import com.wfit.service.impl.CommodityServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/commodity")
@RequiredArgsConstructor
public class CommodityController {
    @Autowired
    private CommodityServiceImpl commodityService;

    @ApiOperation("商品列表查询")
    @GetMapping("/list")
    public Result list(CommodityListDto dto) {
        Result list = commodityService.list(dto);
        return list;
    }

    @ApiOperation("商品新增和更新")
    @PostMapping("/add")
    public Result addCommodity(CommodityDto dto) throws IOException {
        Result result = commodityService.addCommodity(dto);
        return result;
    }


    @GetMapping("/{commodityId}")
    public Result backOneCommodity(@PathVariable String commodityId) {
        Commodity byId = commodityService.backOneCommodity(commodityId);
        return Result.success(byId);
    }

    // 用户端
    @ApiOperation("商品列表查询")
    @GetMapping("/first")
    public Result list() {
        List<Commodity> list = commodityService.list();
        return Result.success(list);
    }

    @ApiOperation("按id查询商品")
    @GetMapping
    public Result getOneCommodity(String goodsId) {
        Commodity byId = commodityService.getById(goodsId);

        return Result.success(byId);
    }
}
